

#include "packet.h"
#include "tsfile.h"
#include "cstring.h"
#include <iostream>
using namespace std;

using namespace NDVBSpace;

int main(int argc,char *argv[])
{
    CPacket pp;
    CTable tbl;
    CTSFile * pts;
    //const char  filename[100] = "ICAM2.x_EcmFilter.ts";
    u32 pktidx = 0;
    u32 ecmcnt = 0;
    CAMessageSection * pCAM = NULL;
    u8	table_id = 0	;

     //TestDataLenth();
    CString str;
     
      cout<<"before"<< str<<endl;
     str = " hello ";
     cout<<"after"<< str<<endl;
     if(1 >= argc)
    {
        printf("please input file name. \n");
        return 0;
    }
     
     pts = new CTSFile((s8 *)argv[1]);
     if(true == pts->TraceTsfileState())
        {
             tbl.SetIds(PROGRAM_ASSOCIATION_INFORMATION, PAT_PID, 0, 0);
               if( pts->BuildTable(tbl))
                {
                        CFile * pFile = new CFile("./pat.txt");
                        tbl.SaveToFile(pFile);
                        printf("save pat version %d. \n",tbl.GetVersion());
                        delete pFile;
                }
               tbl.SetIds(CONDITIONAL_ACCESS_INFORMATION, CAT_PID, 0, 0);
               if( pts->BuildTable(tbl))
                {
                        CFile * pFile = new CFile("./cat.txt");
                        tbl.SaveToFile(pFile);
                        printf("save cat version %d. \n",tbl.GetVersion());
                        delete pFile;
                }

        #if 0
            for(pktidx = 0 ;pktidx < pts->GetPacketCount(); pktidx++)
            {
                pts->ReadPacket(&pp, pktidx);
                if(0x500   == pp.GetPID())
                {
                    //printf("pktidx %d , GetContinuityCounter( %d ). \n", pktidx, pp.GetContinuityCounter());
                    if(pp.GetPayLoadUnitStartIndicator())
                        {
                            pCAM = (CAMessageSection *)pp.GetSectionAddress();
                           if(table_id != pCAM->table_id)
                            {
                                table_id = pCAM->table_id;
                                ecmcnt++;
                                if(table_id == ENTITLE_CONTROL_MESSAGE_EVEN)
                                    printf("pktidx %d ,even ecm\n",pktidx);
                                else if(table_id == ENTITLE_CONTROL_MESSAGE_ODD)
                                    printf("pktidx %d ,odd ecm\n",pktidx);
                                else
                                    printf("pktidx %d ,unknow ecm\n",pktidx);
                            }
                        }
                }
            }
     #endif
        }
    // printf("ecmcnt %d .\n",ecmcnt);
       delete pts;
       return 0;
}




